//
//  EMFirstLayout.m
//  ElectronicMall
//
//  Created by 史伟文 on 15/1/1.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMFirstLayout.h"
#define MARGIN [marginArray[indexPath.section] intValue]
@interface EMFirstLayout()
{
    NSInteger cellCount;
    NSInteger sectionCount;
}
@end
@implementation EMFirstLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
    
    NSMutableArray *attributesArray = [NSMutableArray array];
    
    sectionCount = [[super collectionView] numberOfSections];
    for (int i = 0; i < sectionCount; i ++) {
        cellCount = [[super collectionView] numberOfItemsInSection:i];
        for (int j = 0; j < cellCount; j ++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:j inSection:i];
            [attributesArray addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
        }
        
        
    }
    
    return attributesArray;
    
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *marginArray = @[@0, @H(160), @H(260), @H(480), @H(780)];
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    if (indexPath.section == 0) {
        attributes.frame = CGRectMake(0, 0 + MARGIN, W(320), H(160));
    } else if (indexPath.section == 1) {
        attributes.frame = CGRectMake(0 + indexPath.row * W(80), 0 + MARGIN, W(80), H(100));
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            attributes.frame = CGRectMake(0, 0 + MARGIN, W(320), H(30));
        } else if (indexPath.item == 1) {
            attributes.frame = CGRectMake(0, H(30) + MARGIN, W(140), H(180));
        } else if (indexPath.item == 2) {
            attributes.frame = CGRectMake(W(140), H(30) + MARGIN, W(180), H(90));
        } else if (indexPath.item == 3) {
            attributes.frame = CGRectMake(W(140), H(120) + MARGIN, W(90), H(90));
        } else {
            attributes.frame = CGRectMake(W(230), H(120) + MARGIN, W(90), H(90));
        }
    } else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            attributes.frame = CGRectMake(0, 0 + MARGIN, W(320), H(30));
        } else if (indexPath.item == 1) {
            attributes.frame = CGRectMake(0, H(30) + MARGIN, W(90), H(90));
        } else if (indexPath.item == 2) {
            attributes.frame = CGRectMake(W(90), H(30) + MARGIN, W(90), H(90));
        } else if (indexPath.item == 3) {
            attributes.frame = CGRectMake(W(180), H(30) + MARGIN, W(140), H(180));
        } else if (indexPath.item == 4) {
            attributes.frame = CGRectMake(0, H(120) + MARGIN, W(90), H(90));
        } else if (indexPath.item == 5) {
            attributes.frame = CGRectMake(W(90), H(120) + MARGIN, W(90), H(90));
        } else if (indexPath.item == 6){
            attributes.frame = CGRectMake(0, H(210) + MARGIN, W(160), H(80));
        } else {
            attributes.frame = CGRectMake(W(160), H(210) + MARGIN, W(160), H(80));
        }
        
    } else  {
        if (indexPath.row == 0) {
            attributes.frame = CGRectMake(0, 0 + MARGIN, W(320), H(30));
        } else if (indexPath.item == 1) {
            attributes.frame = CGRectMake(0, H(30) + MARGIN, W(140), H(180));
        } else if (indexPath.item == 2) {
            attributes.frame = CGRectMake(W(140), H(30) + MARGIN, W(180), H(90));
        } else if (indexPath.item == 3) {
            attributes.frame = CGRectMake(W(140), H(120) + MARGIN, W(90), H(90));
        } else if (indexPath.item == 4) {
            attributes.frame = CGRectMake(W(230), H(120) + MARGIN, W(90), H(90));
        } else if (indexPath.item == 5) {
            attributes.frame = CGRectMake(0, H(210) + MARGIN, W(107), H(90));
        } else if (indexPath.item == 6){
            attributes.frame = CGRectMake(W(107), H(210) + MARGIN, W(106), H(90));
        } else {
            attributes.frame = CGRectMake(W(213), H(210) + MARGIN, W(107), H(90));
        }

    }
    
    return attributes;
}
@end

